package com.rtsp.poe.rtsplive555;

import android.media.AudioFormat;

import java.util.concurrent.ArrayBlockingQueue;

import static java.lang.Thread.sleep;

/**
 * author: wsy on 2022/1/20 16:25
 * <p>
 * description: XXX
 */
public class AACDecoderUtil {
    private static final String TAG = "AACDecoderUtil";
    //声道数
    private static int audio_channel_count = 2;
    //采样率
    private static int audio_samplerate = 48000;
    //编码样式
    private static int audio_audioFormat = AudioFormat.ENCODING_PCM_16BIT;
    //用于播放解码后的pcm
    private AACAudioTrack mPlayer;
    //用来记录解码失败的帧数
    private int count = 0;
    private ArrayBlockingQueue<byte[]> aacQueue = new ArrayBlockingQueue<byte[]>(15);
    private boolean isRuning = false;
    private Thread decodeThread;
    private boolean startDecode = false;

    public AACDecoderUtil(int channel, int samplerate) {
        audio_channel_count = channel;
        audio_samplerate = samplerate;
    }

    public AACDecoderUtil(int channel, int samplerate, int audioFormat) {
        audio_channel_count = channel;
        audio_samplerate = samplerate;
        audio_audioFormat = audioFormat;
    }

    /**
     * 初始化所有变量
     */
    public void start() {
        prepare();
    }

    /**
     * 初始化解码器
     *
     * @return 初始化失败返回false，成功返回true
     */
    public boolean prepare() {
        // 初始化AudioTrack
        mPlayer = new AACAudioTrack(audio_samplerate, audio_channel_count/*AudioFormat.CHANNEL_OUT_STEREO*/, audio_audioFormat);
        mPlayer.init();
        return true;
    }

    /**
     * 编码线程
     */
    public void startDecodeThread() {
        isRuning = false;
        decodeThread = new Thread(new Runnable() {
            @Override
            public void run() {
                isRuning = true;
                byte[] input = null;
                while (isRuning) {
                    if (aacQueue.size() > 0) {
                        try {
                            input = aacQueue.poll();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    } else {
                        try {
                            sleep(50);
                        } catch (Exception e) {
                        }
                    }
                }
            }
        });
        decodeThread.setName("SponAACDecode");
        decodeThread.start();
        startDecode = true;
    }

    public void writeData(byte[] buf, int offset, int length) {
        mPlayer.playAudioTrack(buf, 0, length);
//        if (aacQueue.size() >= 10) {
//            aacQueue.poll();
//        }
//        aacQueue.add(buf);
    }


    //返回解码失败的次数
    public int getCount() {
        return count;
    }

    /**
     * 释放资源
     */
    public void stop() {
        isRuning = false;
        startDecode = false;
        try {
            sleep(60);
        } catch (Exception e) {
        }
        if (decodeThread != null) {
            decodeThread = null;
        }
        try {
            if (mPlayer != null) {
                mPlayer.release();
                mPlayer = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
